<%-- 
    Document   : download_submission
    Created on : 2011年8月27日, 上午09:58:44
    Author     : Ming
--%>

<%@page import="unilearn.vo.Role"%>
<%@page import="unilearn.vo.Person"%>
<%@page import="unilearn.bo.AccessValidator"%>
<%@page import="unilearn.bo.SubjectManagement"%>
<%@page import="unilearn.vo.Submission"%>
<%@page import="unilearn.vo.Assignment"%>
<%@page import="unilearn.bo.AssignmentManagement"%>
<%@page import="unilearn.dao.AssignmentDAO"%>
<%@page import="unilearn.vo.Subject"%>
<%@page import="java.util.List"%>
<%@page import="unilearn.dao.SubjectDAO"%>
<%@page import="unilearn.bo.SubmissionManagement"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
            SubmissionManagement submMgt = (SubmissionManagement) context.getBean("submMgt");
            AccessValidator vaildater = (AccessValidator) context.getBean("accessValidator");
            SubjectManagement subjectMgt = (SubjectManagement) context.getBean("subjectMgt");
            AssignmentManagement assignMgt = (AssignmentManagement) context.getBean("assignMgt");
            // Person person = vaildater.validate("hugoboss@gmail.com", "hugob222");
            HttpSession s = request.getSession();


            Person person = (Person) s.getAttribute("person");
            Role role = (Role) s.getAttribute("role");

            if ((person == null) || (role == null)) {
                out.print("person or role is null.");
            }

            out.print("<p>" + person.toString() + "</p>");
            out.print("<p>" + role.toString() + "</p>");
            out.print("<p><hr/></p>");

            boolean isDownload = false;
            List<Subject> subs = null;

            if (role.getName().equals("student")) {
                isDownload = false;
                subs = subjectMgt.getStudentSubjects(person);
            } else if (role.getName().equals("staff")) {
            } else if (role.getName().equals("lecturer")) {
                isDownload = true;
                subs = subjectMgt.getConvernorSubjects(person);
                subs.addAll(subjectMgt.getMarkerSubjects(person));
            } else if (role.getName().equals("administrator")) {
            }


            if (subs == null) {
                return;
            }

            out.print("<p>Subject(" + subs.size() + ")</p>");
            for (Subject sub : subs) {
                List<Assignment> assigns = assignMgt.getAssignments(sub);
                if (assigns == null) {
                    break;
                }

                out.print("<p> + [Subject] " + sub.getSubjectCode() + " : " + sub.getName() + "(" + assigns.size() + ")</p>");
                for (Assignment assign : assigns) {
                    if (isDownload) {
                        List<Submission> subms = submMgt.getSubmissions(assign);
                        if (subms == null) {
                            break;
                        }

                        out.print("<p> + + [Assignment] " + assign.getId() + " : " + assign.getName() + "(" + subms.size() + ")</p>");
                        for (Submission subm : subms) {

                            out.print("<p> + + - [Submission] " + subm.getId() + " : <a href=\"../download_submission.do?submId=" + subm.getId() + "\">" + subm.getFileUrl() + "</a></p>");
                        }
                    } else {
                        out.print("<p> + + [Assignment] " + assign.getId() + " : " + assign.getName() + "</p><br />");
                        out.print("<div>");
                        out.print("<form method=\"post\" enctype=\"multipart/form-data\" action=\"../upload_submission.do\">");
                        out.print("<p>assign id: <input type=\"text\" name=\"assignId\" value=\"" + assign.getId() + "\" /></p>");
                        out.print("<p>autor id: <input type=\"text\" name=\"autorId\" /></p>");
                        out.print("<p>file: <input type=\"file\" name=\"assignFile\" /></p>");
                        out.print("<p><input type=\"submit\" value=\"upload\" /></p>");
                        out.print("</form>");
                        out.print("</div>");
                    }
                }
            }

        %>
    </body>
</html>
